package org.nimi317.web_gis.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.nimi317.web_gis.form.post.KrigingPost;
import org.nimi317.web_gis.dao.KrigingDao;
import org.nimi317.web_gis.entity.Kriging;
import org.nimi317.web_gis.service.KrigingService;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

/**
 * 克里金模型参数(Kriging)表服务实现类
 *
 * @author makejava
 * @since 2024-05-07 13:39:00
 */
@Service("krigingService")
public class KrigingServiceImpl extends ServiceImpl<KrigingDao, Kriging> implements KrigingService {

    private final KrigingService krigingService;

    public KrigingServiceImpl(@Lazy KrigingService krigingService) {
        this.krigingService = krigingService;
    }

    @Override
    public Integer isExist(KrigingPost map) {
        //判断以上对象是否存在
        Integer exist = krigingService.isExistAndGetId(map);
        if (ObjectUtil.isNotNull(exist)) {
            return exist;
        }
        Kriging kriging = new Kriging();
        BeanUtils.copyProperties(map, kriging);
        this.save(kriging);
        return kriging.getId();
    }

    @Override
    public Integer isExistAndGetId(KrigingPost map) {
        LambdaQueryWrapper<Kriging> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Kriging::getId)
                .eq(Kriging::getWidth, map.getWidth())
                .eq(Kriging::getAlpha, map.getAlpha())
                .eq(Kriging::getSigma2, map.getSigma2())
                .eq(Kriging::getRange, map.getRange())
                .eq(Kriging::getField, map.getField())
                .eq(Kriging::getModel, map.getModel());
        Kriging one = this.getOne(wrapper);
        if (ObjectUtil.isEmpty(one)) {
            return null;
        }
        return one.getId();
    }
}

